// 用户--表现层
var express = require('express');
const { md5} = require('../utils/salt');
const {create,get,update,remove,reg,login,isUse} = require("../service/usersService");
var router = express.Router();

//查询数据
router.get('/', async function (req, res, next) {
    const data = await get();
    res.send(data);
});

//新增
router.post("/create", async function (req, res, next) {
    const { username,password } = req.body;
    const newPwd = md5(password);
    const data = await create({username,password:newPwd});
    res.send(data);
});

//更新
router.post("/update", async function (req, res, next) {
    const { _id,username,password} = req.body;
    const data = await update({ _id }, {username,password});
    res.send(data);
});

//删除
router.post("/remove", async function (req, res, next) {
    const { _id } = req.body;
    const data = await remove({ _id });
    res.send(data);
});

// 登录
router.get('/login', async function (req, res, next) {
    const { username, password } = req.query;
    const status = await login({ username, password });
    res.send(status);
});

// 注册
router.post('/reg', async function (req, res, next) {
    const { username, password } = req.body;
    const status = await reg({ username, password });
    res.send(
        status
    );
});

// 验证用户名
router.post('/isUse', async function (req, res, next) {
    const { username } = req.body;
    const status = await isUse({ username });
    res.send(
        status
    );
});

module.exports = router;
